/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server.swing.tools;

import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tools.SearchGenerator;

/**
 *
 * @author Pungin
 */
public class SearchGeneratorFrame extends javax.swing.JFrame {

    /**
     * Creates new form SearchGeneratorFrame
     */
    public SearchGeneratorFrame() {
        initComponents();
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(75);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(275);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
    }

    @Override
    public void setVisible(boolean bln) {
        if (bln) {
            init();
            setLocationRelativeTo(null);
        }
        super.setVisible(bln);
    }

    public void init() {
        jTextField1.setText("");
        jComboBox1.setSelectedIndex(0);
        modelClear();
    }

    public void modelClear() {
        DefaultTableModel model = ((DefaultTableModel) jTable1.getModel());
        int count = model.getRowCount();
        for (int i = 0; i < count; i++) {
            model.removeRow(0);
        }
    }

    public boolean addModel(int type, Map<Integer, String> data, boolean show) {
        return addModel(SearchGenerator.SearchType.valueOf(SearchGenerator.SearchType.nameOf(type)), data, show);
    }

    public boolean addModel(SearchGenerator.SearchType type, Map<Integer, String> data, boolean show) {
        if (data == null || data.isEmpty()) {
            if (show) {
                JOptionPane.showMessageDialog(null, "未找到。");
            }
            return false;
        }
        DefaultTableModel model = ((DefaultTableModel) jTable1.getModel());
        for (int i : data.keySet()) {
            model.addRow(new Object[]{type.name(), data.get(i), i});
        }
        return true;
    }

    public void search() {
        modelClear();
        String str = jTextField1.getText();
        int type = jComboBox1.getSelectedIndex();
        if (type == 0) {
            boolean find = false;
            for (int i = 1; i <= SearchGenerator.裝備套裝; i++) {
                boolean show = addModel(i, SearchGenerator.getSearchData(i, str),
                        i == SearchGenerator.裝備套裝 ? !find : false);
                if (!find) {
                    find = show;
                }
            }
        } else {
            addModel(type, SearchGenerator.getSearchData(type, str), true);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated
    // Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("代碼檢索器");

        jLabel1.setText("檢索類型:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "全部", "道具", "NPC", "地圖", "怪物", "任務", "技能", "職業", "套裝" }));

        jButton1.setText("檢索");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("檢索內容:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "類型", "名稱", "值" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.Integer.class };
            boolean[] canEdit = new boolean[] { false, true, true };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                .createSequentialGroup().addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 374,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 147,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                .createSequentialGroup().addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane1,
                        javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap()));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
        search();
    }// GEN-LAST:event_jButton1ActionPerformed
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
